replaceMultiplicated.ts ➔ replaceMultiplicated   B
last analyzed

Complexity

Conditions 5

Size

Total Lines 36
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 36
rs 8.6693
c 0
b 0
f 0
cc 5
1
export = replaceMultiplicated
2
3
function replaceMultiplicated(
4
  sources: string[],
5
  //TODO `searchValue: string[]` or `replacementMap`
6
  searchValue: string,
7
  replacements: string[]
8
) {
9
  const $return: (string|string[])[] = [...sources]
10
  , {length} = replacements
11
12
  for (let i = $return.length; i--;) {
13
    const line = sources[i]
14
15
    if (!line.includes(searchValue))
16
      continue
17
18
    const replaced: string[] = new Array(length)
19
20
    for (let j = length; j--;) {
21
      let next = line
22
      , pre: string
23
24
      //TODO Change to `.replaceAll` with common polyfill
25
      do {
26
        pre = next
27
        next = pre.replace(searchValue, replacements[j])
28
      } while (next !== pre)
29
30
      replaced[j] = next
31
    }
32
33
    $return[i] = replaced
34
  }
35
36
  //TODO Set up polyfill for `.flat()`
37
  return $return.flat()
38
}
39